﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using FileHelpers;
using System.Data;
using System.Data.SqlClient;


namespace TestJQGrid
{
    public partial class MasterUpload : System.Web.UI.Page
    {
        private static DelimitedFileEngine dfengine;
        private static DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            UploadButton.Enabled = false;
            ResetButton.Enabled = false;
            ErrorButton.Enabled = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            if (!FileUpload1.HasFile)
            {
                return;
            }
            if (TableListDropDown.SelectedValue == null)
            {
                return;
            }

            initEngine(TableListDropDown.SelectedValue);
            dt = getDataTable(FileUpload1.PostedFile);

            GridView1.DataSource = dt;
            GridView1.DataBind();

            TableListDropDown.Enabled = false;
            SeperatorDDL.Enabled = false;
            FileUpload1.Enabled = false;
            Button1.Enabled = false;
            UploadButton.Enabled = true;
            ResetButton.Enabled = true;
        }

        void initEngine(string tableName)
        {
            if (tableName == "Schemes")
            {
                dfengine = new DelimitedFileEngine(typeof(Schemes));
            }
            else if (tableName == "ModelManufacturer")
            {
                dfengine = new DelimitedFileEngine(typeof(ModelManu));
            }
            else if (tableName == "StateCity")
            {
                dfengine = new DelimitedFileEngine(typeof(StateCity));
            }
            else if (tableName == "AgentTaxMaster")
            {
                dfengine = new DelimitedFileEngine(typeof(AgentTaxMaster));
            }
            else if (tableName == "AdhocAdjustments")
            {
                dfengine = new DelimitedFileEngine(typeof(AdhocAdjustments));
            }
            else
            {
                dfengine = null;
            }
        }

        System.Data.DataTable getDataTable(HttpPostedFile fileStream)
        {
            TextReader tr = new StreamReader(fileStream.InputStream);
            System.Data.DataTable dt;

            dfengine.Options.Delimiter = this.SeperatorDDL.SelectedValue;
            dfengine.Options.IgnoreFirstLines = 1;
            //dfengine.Options.RecordCondition.Condition = RecordCondition.ExcludeIfContains
            dfengine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
            dt = dfengine.ReadStreamAsDT(tr);
            if (dfengine.ErrorManager.HasErrors)
            {
                this.ErrorButton.Enabled = true;
            }
            return dt;
        }

        protected void ErrorButton_Click(object sender, EventArgs e)
        {
            if (dfengine.ErrorManager.HasErrors)
            {
                dfengine.ErrorManager.SaveErrors(Server.MapPath("~\\errors.txt"));
                Response.ContentType = "Error File (*.txt)";
                Response.AppendHeader("Content-Disposition", "attachment; filename=errors.txt");
                Response.TransmitFile(Server.MapPath("~\\errors.txt"));
                Response.End();
            }
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (TableListDropDown.SelectedValue.ToString() == "AgentTaxMaster" || TableListDropDown.SelectedValue.ToString() == "AdhocAdjustments")
                {
                    GlobalMethods.uploadDT2DB(dt, TableListDropDown.SelectedValue.ToString(), true);
                }
                else
                {
                    GlobalMethods.uploadDT2DB(dt, TableListDropDown.SelectedValue.ToString());
                }
                
                WebMsgBox.Show("Upload Success.");
            }
            catch (SqlException)
            {
                WebMsgBox.Show("Upload Success. Duplicate entries are ignored");
            }
            ResetButton_Click(sender, e);
        }

        protected void ResetButton_Click(object sender, EventArgs e)
        {
            TableListDropDown.Enabled = true;
            SeperatorDDL.Enabled = true;
            FileUpload1.Enabled = true;
            Button1.Enabled = true;
            ErrorButton.Enabled = false;
            UploadButton.Enabled = false;
            ResetButton.Enabled = false;

            dt = null;
            dfengine = null;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}